package com.ruoyi.quartz.task;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.quartz.util.GetuiUtils;
import com.ruoyi.system.domain.CrmJob;
import com.ruoyi.system.domain.CrmUser;
import com.ruoyi.system.domain.CrmUserRecord;
import com.ruoyi.system.domain.SysUserNotice;
import com.ruoyi.system.domain.dto.UserNoticeCountDTO;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.req.Audience;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;
import com.getui.push.v2.sdk.dto.req.message.android.GTNotification;
/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysUserNoticeService sysUserNoticeService;
    //扫描生日
    @Autowired
    private ICrmUserService crmCustomerService;
    //扫描事件提醒
    @Autowired
    private ICrmJobService crmJobService;

    //扫描跟进提醒
    @Autowired
    private ICrmUserRecordService crmUserRecordService;


    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }

    public void ryNoticeCrmJob()
    {

        System.out.println("执行任务提醒：");
        List<SysUser> sysUserList=userService.selectUserListAll();
        for (SysUser sysUser1:sysUserList) {
            if(sysUser1.getCid()==null || StringUtils.isEmpty(sysUser1.getCid())){
                continue;
            }
            //任务提醒
            List<CrmJob> crmJobList = crmJobService.selectCrmJobByMinuteNotice(sysUser1.getUserId(),10);
            if (crmJobList != null && crmJobList.size() > 0) {
                for(CrmJob crmJob:crmJobList){
                    toMessage("消息提醒", crmJob.getJobName(), "url", "https://www.getui.com", sysUser1.getCid());

                }
            }
        }
    }

    public void ryNotice()
    {
        System.out.println("执行消息提醒任务：");
         List<SysUser> sysUserList=userService.selectUserListAll();
        for (SysUser sysUser1:sysUserList){
            if(sysUser1.getCid()==null || StringUtils.isEmpty(sysUser1.getCid())){
                continue;
            }
            List<CrmUser> list=  crmCustomerService.selectCrmUserListByBirthDayByDay(3,sysUser1.getUserId());
            if(list!=null && list.size()>0) {
                SysUserNotice sysUserNotice = new SysUserNotice();
                sysUserNotice.setCreateUser(sysUser1.getUserId());
                sysUserNotice.setIsread(0L);
                sysUserNotice.setCreateTime(new Date());
                sysUserNotice.setContent("未来3天有" + list.size() + "个过生日的人哦");
                sysUserNoticeService.insertSysUserNotice(sysUserNotice);
                toMessage("消息提醒",sysUserNotice.getContent(),"url","https://www.getui.com",sysUser1.getCid());
            }
            //任务提醒
            List<CrmJob> crmJobList=  crmJobService.selectCrmJobByTodayNotice(sysUser1.getUserId());
            if(crmJobList!=null && crmJobList.size()>0) {
                SysUserNotice sysUserNotice = new SysUserNotice();
                sysUserNotice.setCreateUser(sysUser1.getUserId());
                sysUserNotice.setIsread(0L);
                sysUserNotice.setCreateTime(new Date());
                sysUserNotice.setContent("今日有" + crmJobList.size() + "个事件需要留意");
                sysUserNoticeService.insertSysUserNotice(sysUserNotice);
                toMessage("消息提醒",sysUserNotice.getContent(),"url","https://www.getui.com",sysUser1.getCid());
            }
            //跟进提醒
            List<CrmUserRecord> crmUserRecordList=  crmUserRecordService.selectCrmUserRecordListByNextTime(sysUser1.getUserId());
            if(crmUserRecordList!=null && crmUserRecordList.size()>0) {
                SysUserNotice sysUserNotice = new SysUserNotice();
                sysUserNotice.setCreateUser(sysUser1.getUserId());
                sysUserNotice.setIsread(0L);
                sysUserNotice.setCreateTime(new Date());
                sysUserNotice.setContent("今日有" + list.size() + "个客户跟进需要留意");
                sysUserNoticeService.insertSysUserNotice(sysUserNotice);
                toMessage("消息提醒",sysUserNotice.getContent(),"url","https://www.getui.com",sysUser1.getCid());
            }
        }
    }


    public void toMessage(String title,String content,String clickType,String url,String cid){


        System.out.println("toMessage=====cid=========="+cid);

        //根据cid进行单推
        PushDTO<Audience> pushDTO = new PushDTO<Audience>();
        // 设置推送参数
        pushDTO.setRequestId(System.currentTimeMillis() + "");
        /**** 设置个推通道参数 *****/
        PushMessage pushMessage = new PushMessage();
        pushDTO.setPushMessage(pushMessage);
        GTNotification notification = new GTNotification();
        pushMessage.setNotification(notification);
        notification.setTitle(title);
        notification.setBody(content);
        notification.setClickType("payload");
        notification.setPayload("1");
        /**** 设置个推通道参数，更多参数请查看文档或对象源码 *****/
        /*设置接收人信息*/
        Audience audience = new Audience();
        pushDTO.setAudience(audience);
        audience.addCid(cid);
        /*设置接收人信息结束*/
        // 进行cid单推
        PushApi pushApi= GetuiUtils.getPushApi();
        ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO);
        if (apiResult.isSuccess()) {
            // success
            System.out.println(apiResult.getData());
        } else {
            // failed
            System.out.println("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());
        }
    }
}
